<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
    <base href="/oa1/" />
    <title>已发消息 - 襄阳蓝芯智能化办公系统</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="static/bootstrap-3.3.5/css/bootstrap.css" rel="stylesheet">
    <style>
        .thead-blue {
            color: #fff;
            font-weight: bold;
            background-color: #337ab7;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .pagination {
            margin: 10px 0;
        }
        .modal-body {
            padding: 20px;
        }
    </style>
    <script src="static/jquery/jquery-1.10.2.min.js"></script>
    <script src="static/bootstrap-3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <!-- 搜索区域 -->
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h4 class="panel-title">已发消息搜索</h4>
        </div>
        <div class="panel-body">
            <form action="xx/duanxiaoxi/showMsg" id="searchForm" method="post" class="form-horizontal" role="form">
                <div class="form-group">
                    <div class="col-md-5">
                        <label for="biaoti" class="col-md-4 control-label">消息主题</label>
                        <div class="col-md-8">
                            <input type="text" id="biaoti" name="biaoti" class="form-control"
                                   placeholder="请输入消息主题" value="${query.biaoti}">
                        </div>
                    </div>
                    <div class="col-md-5">
                        <label for="empName" class="col-md-4 control-label">接收人</label>
                        <div class="col-md-8">
                            <input type="hidden" id="empids" name="empids" value="${query.empids}">
                            <div class="input-group">
                                <input type="text" id="empName" name="empName" readonly class="form-control"
                                       placeholder="请选择员工" value="${query.empName}">
                                <span class="input-group-btn">
                                    <button class="btn btn-info" type="button" data-toggle="modal" data-target="#selectempModal">选择</button>
                                </span>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <a href="xx/duanxiaoxi/toAddMsg" class="btn btn-info">发送消息</a>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-5">
                        <label for="startDate" class="col-md-4 control-label">开始时间</label>
                        <div class="col-md-8">
                            <input type="date" id="startDate" name="startDate" class="form-control"
                                   value="${query.startDate}">
                        </div>
                    </div>
                    <div class="col-md-5">
                        <label for="endDate" class="col-md-4 control-label">结束时间</label>
                        <div class="col-md-8">
                            <input type="date" id="endDate" name="endDate" class="form-control"
                                   value="${query.endDate}">
                        </div>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-success">搜索</button>
                        <button type="button" onclick="resetForm()" class="btn btn-default">重置</button>
                    </div>
                </div>
                <input type="hidden" name="page" id="page" value="${allXiaoxi.current}">
                <input type="hidden" name="pageSize" id="pageSize" value="${allXiaoxi.size}">
            </form>
        </div>
    </div>

    <!-- 列表区域 -->
    <div class="panel panel-primary">
        <table class="table table-bordered table-hover">
            <thead>
            <tr class="thead-blue text-center">
                <td>消息标题</td>
                <td>发送时间</td>
                <td>接收人</td>
                <td>操作</td>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${allXiaoxi.records}" var="xiaoxi">
                <tr align="center">
                    <td>${xiaoxi.biaoti}</td>
                    <td>${xiaoxi.createDate}</td>
                    <td>
                        <c:forEach items="${xiaoxi.xiaoxijieshou}" var="jieshou">
                            <span class="text-success">${jieshou.empVo1.empName}</span>
                        </c:forEach>
                    </td>
                    <td>
                        <a type="button" href="xx/duanxiaoxi/msgInfo?oid=${xiaoxi.oid}"
                           class="btn btn-info btn-xs">查看详情</a>
                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </div>

    <!-- 分页区域 -->
    <div class="text-center">
        <ul class="pagination">
            <li><a href="javascript:gotoPage(1)">首页</a></li>
            <li <c:if test="${allXiaoxi.current == 1}">class="disabled"</c:if>>
                <a href="javascript:gotoPage(${allXiaoxi.current - 1})">上一页</a>
            </li>
            <li class="disabled"><a href="javascript:;">第${allXiaoxi.current}/${allXiaoxi.pages}页</a></li>
            <li <c:if test="${allXiaoxi.current == allXiaoxi.pages}">class="disabled"</c:if>>
                <a href="javascript:gotoPage(${allXiaoxi.current + 1})">下一页</a>
            </li>
            <li><a href="javascript:gotoPage(${allXiaoxi.pages})">尾页</a></li>
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="javascript:;">
                    每页${allXiaoxi.size}条 <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="javascript:changePageSize(5)">5条/页</a></li>
                    <li><a href="javascript:changePageSize(10)">10条/页</a></li>
                    <li><a href="javascript:changePageSize(15)">15条/页</a></li>
                </ul>
            </li>
        </ul>
    </div>
</div>

<!-- 选择员工模态框 -->
<div class="modal fade" id="selectempModal" tabindex="-1" role="dialog" aria-labelledby="selectempModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="selectempModalLabel">选择员工</h4>
            </div>
            <div class="modal-body">
                <form action="" class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="dept" class="col-md-4 control-label">所属部门</label>
                        <div class="col-md-5">
                            <select name="dept" class="form-control" id="dept">
                                <option value="">--请选择--</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="job" class="col-md-4 control-label">所属职位</label>
                        <div class="col-md-5">
                            <select name="job" class="form-control" id="job">
                                <option value="">--请选择--</option>
                            </select>
                            <div id="jobLoading" class="loading" style="display: none;">加载中...</div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="emp" class="col-md-4 control-label">员工信息</label>
                        <div class="col-md-5">
                            <select name="emp" class="form-control" id="emp" size="10">
                                <option value="" disabled>------请先选择部门和职位------</option>
                            </select>
                            <div id="empLoading" class="loading" style="display: none;">加载中...</div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="sub" class="btn btn-primary">确认选择</button>
            </div>
        </div>
    </div>
</div>

<script>
    // 分页跳转
    function gotoPage(pageNum) {
        if (pageNum < 1 || pageNum > ${allXiaoxi.pages}) return;
        $("#page").val(pageNum);
        $("#searchForm").submit();
    }

    // 切换每页条数
    function changePageSize(size) {
        $("#pageSize").val(size);
        $("#page").val(1);
        $("#searchForm").submit();
    }

    // 重置表单
    function resetForm() {
        $("#biaoti").val("");
        $("#empids").val("");
        $("#empName").val("");
        $("#startDate").val("");
        $("#endDate").val("");
        $("#searchForm").submit();
    }

    // 页面加载完成后初始化
    $(document).ready(function(){
        // 加载部门列表（一级联动）
        $.getJSON("hr/zz-department/listDeptUseSelect", function(listdept) {
            var deptSelect = $("#dept")[0];
            deptSelect.options.length = 1; // 保留"--请选择--"
            if (listdept && listdept.length > 0) {
                for (var i = 0; i < listdept.length; i++) {
                    var dept = listdept[i];
                    deptSelect.options.add(new Option(dept.deptName, dept.oid));
                }
            } else {
                deptSelect.options.add(new Option("暂无部门数据", ""));
            }
        }).fail(function() {
            alert("部门数据加载失败，请刷新重试");
        });

        // 部门变更时加载职位（二级联动）和员工（三级联动）
        $("#dept").change(function() {
            var deptId = $(this).val();
            // 重置职位和员工选择框
            resetSelect("job", "------请选择职位------");
            resetSelect("emp", "------请先选择职位------");

            if (!deptId) {
                return; // 未选择部门时不加载
            }

            // 显示职位加载状态
            $("#jobLoading").show();
            // 加载职位列表
            $.getJSON("hr/zz-department-job/listDeptJobUseSelect", { deptid: deptId }, function(listjob) {
                $("#jobLoading").hide(); // 隐藏加载状态
                var jobSelect = $("#job")[0];
                jobSelect.options.length = 1; // 保留"--请选择--"

                if (listjob && listjob.length > 0) {
                    for (var i = 0; i < listjob.length; i++) {
                        var job = listjob[i];
                        jobSelect.options.add(new Option(job.jobName, job.jobid));
                    }
                } else {
                    jobSelect.options.add(new Option("该部门暂无职位", ""));
                }
                // 部门变更后自动加载对应员工（基于默认职位）
                loadEmployees();
            }).fail(function() {
                $("#jobLoading").hide();
                alert("职位数据加载失败");
            });
        });

        // 职位变更时加载员工（三级联动）
        $("#job").change(loadEmployees);

        // 加载员工列表的通用方法
        function loadEmployees() {
            var deptId = $("#dept").val();
            var jobId = $("#job").val();
            // 重置员工选择框
            resetSelect("emp", "------加载中...------");
            // 显示员工加载状态
            $("#empLoading").show();

            if (!deptId || !jobId) {
                $("#empLoading").hide();
                resetSelect("emp", "------请先选择部门和职位------");
                return;
            }

            // 加载员工列表
            $.getJSON("hr/emp/listEmpUseSelect", {
                deptid: deptId,
                jobid: jobId
            }, function(emps) {
                $("#empLoading").hide();
                var empSelect = $("#emp")[0];
                empSelect.options.length = 0; // 清空现有选项

                if (emps && emps.length > 0) {
                    for (var i = 0; i < emps.length; i++) {
                        var emp = emps[i];
                        empSelect.options.add(new Option(emp.empName, emp.oid));
                    }
                } else {
                    empSelect.options.add(new Option("该职位暂无员工", ""));
                }
            }).fail(function() {
                $("#empLoading").hide();
                alert("员工数据加载失败");
            });
        }

        // 重置选择框的通用方法
        function resetSelect(selectId, defaultText) {
            var select = $("#" + selectId)[0];
            select.options.length = 0;
            select.options.add(new Option(defaultText, ""));
        }

        // 确认选择员工
        $("#sub").click(function() {
            var empSelect = $("#emp")[0];
            var selectedOption = empSelect.options[empSelect.selectedIndex];
            if (!selectedOption || selectedOption.value === "") {
                alert("请选择有效员工");
                return;
            }
            // 设置选中的员工到搜索框
            $("#creator").val(selectedOption.value);
            $("#empName").val(selectedOption.text);
            $('#selectempModal').modal('hide');
        });

        // 打开员工选择模态框时重置
        $('#selectempModal').on('show.bs.modal', function() {
            resetSelect("dept", "--请选择--");
            resetSelect("job", "--请选择--");
            resetSelect("emp", "------请先选择部门和职位------");
            // 重新加载部门列表（避免缓存旧数据）
            $.getJSON("hr/zz-department/listDeptUseSelect", function(listdept) {
                var deptSelect = $("#dept")[0];
                deptSelect.options.length = 1;
                if (listdept && listdept.length > 0) {
                    for (var i = 0; i < listdept.length; i++) {
                        var dept = listdept[i];
                        deptSelect.options.add(new Option(dept.deptName, dept.oid));
                    }
                } else {
                    deptSelect.options.add(new Option("暂无部门数据", ""));
                }
            });
        });
    });
</script>
</body>
</html>